﻿//Please add references
using CardRegisterCollection.DBUtility;
using CardRegisterCollection.Model;
using System.Data.OleDb;
using System.Data;
using System.Text;
using System;

namespace CardRegisterCollection.DAL
{
    /// <summary>
    /// 数据访问类:car
    /// </summary>
    public partial class Car
	{
        public Car()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperOleDb.GetMaxID("ID", "unit");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from car");
            strSql.Append(" where DeleteFlag = 0 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            return DbHelperOleDb.Exists(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Car model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into car(");
            strSql.Append("ID,CarNumber,DriverName,DriverIDNumber,Telephone,UseUnit,CardType,Remark,CreatedBy,CreatedAt,DeleteFlag)");
            strSql.Append(" values (");
            strSql.Append("@ID,@CarNumber,@DriverName,@DriverIDNumber,@Telephone,@UseUnit,@CardType,@Remark,@CreatedBy,@CreatedAt,@DeleteFlag)");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@ID", OleDbType.VarChar,255),
                    new OleDbParameter("@CarNumber", OleDbType.VarChar,255),
                    new OleDbParameter("@DriverName", OleDbType.VarChar,255),
                    new OleDbParameter("@DriverIDNumber", OleDbType.VarChar,255),
                    new OleDbParameter("@Telephone", OleDbType.VarChar,255),
                    new OleDbParameter("@UseUnit", OleDbType.VarChar,255),
                    new OleDbParameter("@CardType", OleDbType.VarChar,255),
                    new OleDbParameter("@Remark", OleDbType.VarChar,255),
                    new OleDbParameter("@CreatedBy", OleDbType.VarChar,255),
                    new OleDbParameter("@CreatedAt", OleDbType.Date),
                    new OleDbParameter("@DeleteFlag", OleDbType.Boolean,1),
            };
            parameters[0].Value = model.ID;
            parameters[1].Value = model.CarNumber;
            parameters[2].Value = model.DriverName;
            parameters[3].Value = model.DriverIDNumber;
            parameters[4].Value = model.Telephone;
            parameters[5].Value = model.UseUnit;
            parameters[6].Value = model.CardType;
            parameters[7].Value = model.Remark;
            parameters[8].Value = model.CreatedBy;
            parameters[9].Value = model.CreatedAt;
            parameters[10].Value = model.DeleteFlag;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Car model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [car] set ");
            strSql.Append("CarNumber=@CarNumber,");
            strSql.Append("DriverName=@DriverName,");
            strSql.Append("DriverIDNumber=@DriverIDNumber,");
            strSql.Append("Telephone=@Telephone,");
            strSql.Append("UseUnit=@UseUnit,");
            strSql.Append("CardType=@CardType,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("LastUpdatedBy=@LastUpdatedBy,");
            strSql.Append("LastUpdatedAt=@LastUpdatedAt");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@CarNumber",OleDbType.VarChar,255),
                    new OleDbParameter("@DriverName",OleDbType.VarChar,255),
                    new OleDbParameter("@DriverIDNumber",OleDbType.VarChar,255),
                    new OleDbParameter("@Telephone",OleDbType.VarChar,255),
                    new OleDbParameter("@UseUnit",OleDbType.VarChar,255),
                    new OleDbParameter("@CardType",OleDbType.VarChar,255),
                    new OleDbParameter("@Remark",OleDbType.VarChar,255),
                    new OleDbParameter("@LastUpdatedBy", OleDbType.VarChar,255),
                    new OleDbParameter("@LastUpdatedAt", OleDbType.Date),
                    new OleDbParameter("@ID", OleDbType.VarChar,255)};

            parameters[0].Value = model.CarNumber;
            parameters[1].Value = model.DriverName;
            parameters[2].Value = model.DriverIDNumber;
            parameters[3].Value = model.Telephone;
            parameters[4].Value = model.UseUnit;
            parameters[5].Value = model.CardType;
            parameters[6].Value = model.Remark;
            parameters[7].Value = CurrentLoginInfo.UserInfo.UserName;
            parameters[8].Value = DateTime.Now;
            parameters[9].Value = model.ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from car ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@ID",OleDbType.VarChar,255)
            };
            parameters[0].Value = ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Car GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ParentId,Name,Type,CreatedBy,CreatedAt,DeleteFlag,LastUpdatedBy,LastUpdatedAt from unit ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@ID", OleDbType.Integer,4)
            };
            parameters[0].Value = ID;

            Model.Unit model = new Model.Unit();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Car DataRowToModel(DataRow row)
        {
            Model.Car model = new Model.Car();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = row["ID"].ToString();
                }
                //if (row["ParentId"] != null)
                //{
                //    model.ParentId = row["ParentId"].ToString();
                //}
                //if (row["Name"] != null)
                //{
                //    model.Name = row["Name"].ToString();
                //}
                //if (row["Type"] != null && row["Type"].ToString() != "")
                //{
                //    model.Type = int.Parse(row["Type"].ToString());
                //}
                //if (row["CreatedBy"] != null)
                //{
                //    model.CreatedBy = row["CreatedBy"].ToString();
                //}
                //if (row["CreatedAt"] != null && row["CreatedAt"].ToString() != "")
                //{
                //    model.CreatedAt = DateTime.Parse(row["CreatedAt"].ToString());
                //}
                //if (row["DeleteFlag"] != null && row["DeleteFlag"].ToString() != "")
                //{
                //    if ((row["DeleteFlag"].ToString() == "1") || (row["DeleteFlag"].ToString().ToLower() == "true"))
                //    {
                //        model.DeleteFlag = true;
                //    }
                //    else
                //    {
                //        model.DeleteFlag = false;
                //    }
                //}
                //if (row["LastUpdatedBy"] != null)
                //{
                //    model.LastUpdatedBy = row["LastUpdatedBy"].ToString();
                //}
                //if (row["LastUpdatedAt"] != null && row["LastUpdatedAt"].ToString() != "")
                //{
                //    model.LastUpdatedAt = DateTime.Parse(row["LastUpdatedAt"].ToString());
                //}
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BatchNo,CarNumber,DriverName,DriverIDNumber,Telephone,UseUnit,CardType,Remark,UserDefine1,UserDefine2,UserDefine3,UserDefine4,UserDefine5,UserDefine6,UserDefine7,UserDefine8,UserDefine9,UserDefine10,CreatedBy,CreatedAt,DeleteFlag,LastUpdatedBy,LastUpdatedAt ");
            strSql.Append(" FROM car ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM unit ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from unit T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OleDbParameter[] parameters = {
					new OleDbParameter("@tblName", OleDbType.VarChar, 255),
					new OleDbParameter("@fldName", OleDbType.VarChar, 255),
					new OleDbParameter("@PageSize", OleDbType.Integer),
					new OleDbParameter("@PageIndex", OleDbType.Integer),
					new OleDbParameter("@IsReCount", OleDbType.Boolean),
					new OleDbParameter("@OrderType", OleDbType.Boolean),
					new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
					};
			parameters[0].Value = "unit";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

